﻿// -----------------------------------------------------------------------
// <copyright file="TaskLogAttachedFileHandler.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace MyTime
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.IO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class TaskLogAttachedFileHandler
    {
        
        public TaskLogAttachedFileHandler()
        {

        }

        public static string SaveFiles(ObservableCollection<string> fileList, string DataFolder)
        {
            string folder = DataFolder;
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);

            foreach (var item in fileList)
            {
                if (Directory.Exists(item))
                {
                    DirectoryInfo dr = new DirectoryInfo(item);
                    string newtarget= folder + "\\" + dr.Name;
                    if (!Directory.Exists(newtarget))
                        Directory.CreateDirectory(newtarget);
                    CopyDirectory(dr.FullName, newtarget);
                }
                else if(File.Exists(item))
                {
                    string fileName = System.IO.Path.GetFileName(item);
                    string destFile = System.IO.Path.Combine(folder, fileName);
                    System.IO.File.Copy(item, destFile, true);
                }
            }
            DirectoryInfo drr = new DirectoryInfo(folder);
            return drr.FullName;
        }

        public static void CopyDirectory(string sourcePath, string targetPath)
        {
            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath);
            if (Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath,"*",SearchOption.TopDirectoryOnly);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    string fileName = System.IO.Path.GetFileName(s);
                    string destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }

                //copy sub directories
                string[] folders = Directory.GetDirectories(sourcePath, "*", SearchOption.TopDirectoryOnly);

                foreach (var item in folders)
                {
                    DirectoryInfo dr=new DirectoryInfo(item);
                    string newtarget=targetPath + "\\" + dr.Name;

                    CopyDirectory(item, newtarget);
                }

            }
        }

    }
}
